package com.buaa.ImageRoate;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private int minWidth=80;
    private ImageView imageView;
    private TextView textView1,textView2;
    private Matrix matrix=new Matrix();
    private SeekBar seekBar1,seekBar2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView)findViewById(R.id.imageview);
        seekBar1=(SeekBar)findViewById(R.id.seekbar1);
        seekBar2=(SeekBar)findViewById(R.id.seekbar2);
        textView1=(TextView)findViewById(R.id.textview1);
        textView2=(TextView)findViewById(R.id.textview2);
        seekBar1.setOnSeekBarChangeListener(this);
        seekBar2.setOnSeekBarChangeListener(this);
        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        seekBar1.setMax(dm.widthPixels-minWidth);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar.getId()==R.id.seekbar1){
            int newWidth=progress+minWidth;
            int newHeight=(int)(newWidth*3/4);//按照原图进行缩放
            imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
            textView1.setText("图像宽度："+newWidth+"  图像高度："+newHeight);
        }else if(seekBar.getId()==R.id.seekbar2){
            Bitmap bitmap=((BitmapDrawable)(getResources().getDrawable(R.drawable.handsome))).getBitmap();
            matrix.setRotate(progress);
            bitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            imageView.setImageBitmap(bitmap);
            textView2.setText("旋转了"+progress+"度");
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
